package com.lastingwar.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lastingwar.mall.product.dao.BrandDao;
import com.lastingwar.mall.product.dao.CategoryDao;
import com.lastingwar.mall.product.entity.BrandEntity;
import com.lastingwar.mall.product.entity.CategoryEntity;
import com.lastingwar.mall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lastingwar.common.utils.PageUtils;
import com.lastingwar.common.utils.Query;

import com.lastingwar.mall.product.dao.CategoryBrandRelationDao;
import com.lastingwar.mall.product.entity.CategoryBrandRelationEntity;
import com.lastingwar.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    BrandService brandService;

    @Autowired
    CategoryBrandRelationDao relationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存品牌和分类id时同时保存名称
     * @param categoryBrandRelation 品牌和分类关联表
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catalogId = categoryBrandRelation.getCatelogId();
        //查询详细的名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catalogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    /**
     * 请求对品牌进行信息更改时同时更新关联表的品牌信息
     * @param brandId 品牌ID
     * @param name 品牌名称
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        //手动添加Wrapper的方法
        //创建一个ID和名称的关联类
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        //添加一个更新的判断条件，只要品牌ID相同就更新所有关联表中的内容
        this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    /**
     * 对分类进行请求更改时，同时修改关联的分类信息
     * @param catId 分类ID
     * @param name 分类名称
     */
    @Override
    public void updateCategory(Long catId, String name) {
        //使用mybatis 的baseMapper方法
        this.baseMapper.updateCategory(catId,name);
    }

    /**
     * 通过分类ID查出所以品牌的实体类
     * @param catId 分类ID
     * @return  所以相关品牌的集合
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {

        List<CategoryBrandRelationEntity> catelogId = relationDao
                .selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        //使用流做映射把分类ID转化为品牌实体类
        List<BrandEntity> collect = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }
}